# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from market.models import UserProfile, Player, BetRoom
from market.playerData import PlayerData



###########################################################################################
# this controller is the one calling the view
# if the return is true, the player as been auteniied, otherwise we need to return the alternative response
def authentificateUser(request, alternative_response ):

	# authentification
	if not request.user.is_authenticated():
		alternative_response.append( HttpResponseRedirect('/login/?next=%s' % request.path) )
                return False

        # finaly get the player
        return True


###########################################################################################
# this controller is the one calling the view
# if the return is true, the player as been auteniied, otherwise we need to return the alternative response
def authentificateUserWithBetroom(request, betroom, alternative_response ):

	# authentification
	if not request.user.is_authenticated():
		alternative_response.append( HttpResponseRedirect('/login/?next=%s' % request.path) )
                return False

        try:
                betroomName = request.POST['betroom']
        except Exception:
                betroomName = request.user.get_profile().nameOfTheLastVistedBetRoom

        try:
                betroom.append( BetRoom.objects.get(name = betroomName) )
        except Exception:
                alternative_response.append( HttpResponse( 'You need to pass the betroom name or at least to be logged' ))
                return False

        # finaly get the player
        return True

###########################################################################################
# this controller is the one calling the view
# if the return is true, the player as been auteniied, otherwise we need to return the alternative response
def authentificatePlayer(request, player, alternative_response ):

        if( not ( authentificateUser(request, alternative_response ) )):
                return False

        #get the last visted betroom
        betroomName = request.user.get_profile().nameOfTheLastVistedBetRoom

        try:
                if betroomName == "none":
                        # if the user has only a bet room, we use the player of this betroom
                        players = Player.objects.filter( userBeingThisPlayer = request.user )
                        if len( players ) == 1:
                                request.user.get_profile().nameOfTheLastVistedBetRoom = players[0].betroom.name
                                request.user.get_profile().save()
                                player.append( PlayerData(request.user, players[0].betroom ) )
                                return True
                        # else there is an ambiguity and we redirect the user where he can select the correct betroom
                        alternative_response.append( HttpResponseRedirect('/betrooms/') )
                        return False
                
                # finaly get the player
                player.append( PlayerData(request.user, BetRoom.objects.get( name = betroomName ) ) )
                return True
        
        except:
                alternative_response.append( HttpResponseRedirect('/betrooms/') )
                return False
                
